package com.syj.collect.app;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.amap.api.location.AMapLocation;
import com.syj.collect.base.BaseActivity;
import com.syj.collect.fragment.HomeFragment;
import com.syj.collect.fragment.MyFragment;
import com.syj.collect.fragment.NewsFragment;
import com.syj.collect.model.LoginActivity;
import com.zhihu.matisse.Matisse;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {
    private static final String TAG = "MainActivity";
    @BindView(R.id.rb_home)
    RadioButton rbHome;
    @BindView(R.id.rb_message)
    RadioButton rbMessage;
    @BindView(R.id.rb_my)
    RadioButton rbMy;
    @BindView(R.id.rg_main)
    RadioGroup rgMain;
    @BindView(R.id.rl_container)
    FrameLayout rlContainer;
    private HomeFragment homeFragment;
    private NewsFragment newsFragment;
    private MyFragment myFragment;
    public FragmentManager fm;
    private boolean isLogin = false;
    private String tag1 = "HomeFragment";
    private String tag2 = "NewsFragment";
    private String tag3 = "MyFragment";

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
     /*   if (!isLogin) {
            StartAction();
        }*/
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        fm = getSupportFragmentManager();
        showFragment(0);
        setLister();
    }

    private void setLister() {
        rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_home:
                        showFragment(0);
                        break;
                    case R.id.rb_message:
                        showFragment(1);
                        break;
                    case R.id.rb_my:
                        showFragment(2);
                        break;
                }
            }
        });
    }

    public void showFragment(int num) {
        FragmentTransaction fTransaction = fm.beginTransaction();
        hideFragment(fTransaction);
        switch (num) {
            case 0:
                if (homeFragment != null) {
                    fTransaction.show(homeFragment);
                } else {
                    homeFragment = new HomeFragment();
                    fTransaction.add(R.id.rl_container, homeFragment, tag1);
                }
                break;
            case 1:
                if (newsFragment != null) {
                    fTransaction.show(newsFragment);

                } else {
                    newsFragment = new NewsFragment();
                    fTransaction.add(R.id.rl_container, newsFragment, tag2);
                }
                break;
            case 2:
                if (myFragment != null) {
                    fTransaction.show(myFragment);
                } else {
                    myFragment = new MyFragment();
                    fTransaction.add(R.id.rl_container, myFragment, tag3);
                }
                break;
        }
        fTransaction.commitAllowingStateLoss();
    }

    private void hideFragment(FragmentTransaction fTransaction) {
        if (homeFragment != null) {
            fTransaction.hide(homeFragment);
        }
        if (newsFragment != null) {
            fTransaction.hide(newsFragment);
        }
        if (myFragment != null) {
            fTransaction.hide(myFragment);
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        Log.e("results", requestCode + "   " + resultCode+data);
//        Log.e("results", resultCode + "   " + resultCode+data);
//        Log.e("results", data + "   " + resultCode+data);


        homeFragment.onActivityResult(requestCode, resultCode, data);

    }


}
